package com.ruoyi.region.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.region.mapper.TSysAreaMapper;
import com.ruoyi.region.domain.TSysArea;
import com.ruoyi.region.service.ITSysAreaService;

/**
 * 地区设置Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-14
 */
@Service
public class TSysAreaServiceImpl implements ITSysAreaService 
{
    @Autowired
    private TSysAreaMapper tSysAreaMapper;



    @Autowired
    private RedisCache redisCache;

    /**
     * 查询地区设置
     * 
     * @param id 地区设置主键
     * @return 地区设置
     */
    @Override
    public TSysArea selectTSysAreaById(Long id)
    {
        return tSysAreaMapper.selectTSysAreaById(id);
    }

    /**
     * 查询地区设置列表
     * 
     * @param tSysArea 地区设置
     * @return 地区设置
     */
    @Override
    public List<TSysArea> selectTSysAreaList(TSysArea tSysArea)
    {
        return tSysAreaMapper.selectTSysAreaList(tSysArea);
    }

    /**
     * 新增地区设置
     * 
     * @param tSysArea 地区设置
     * @return 结果
     */
    @Override
    public int insertTSysArea(TSysArea tSysArea)
    {
        return tSysAreaMapper.insertTSysArea(tSysArea);
    }

    /**
     * 修改地区设置
     * 
     * @param tSysArea 地区设置
     * @return 结果
     */
    @Override
    public int updateTSysArea(TSysArea tSysArea)
    {
        return tSysAreaMapper.updateTSysArea(tSysArea);
    }

    /**
     * 批量删除地区设置
     * 
     * @param ids 需要删除的地区设置主键
     * @return 结果
     */
    @Override
    public int deleteTSysAreaByIds(Long[] ids)
    {
        return tSysAreaMapper.deleteTSysAreaByIds(ids);
    }

    /**
     * 删除地区设置信息
     * 
     * @param id 地区设置主键
     * @return 结果
     */
    @Override
    public int deleteTSysAreaById(Long id)
    {
        return tSysAreaMapper.deleteTSysAreaById(id);
    }


    /**
     * 通用地域列表
     * @return
     */
    @Override
    public List<Map<String, Object>> regionAreaList() {

        List<Map<String,Object>> provinceList = new ArrayList<>();
        List<Object> list = redisCache.getCacheList("areaList");
        if( list.size()>0){
            provinceList = redisCache.getCacheList("areaList");
        }else{
            provinceList = tSysAreaMapper.selectAreaListByType("0",null);
            for (Map<String, Object> province : provinceList) {
                List<Map<String, Object>> cityList = tSysAreaMapper.selectAreaListByType("1", province.get("code") + "");
                for (Map<String, Object> city : cityList) {
                    List<Map<String, Object>> areaList = tSysAreaMapper.selectAreaListByType("2", city.get("code") + "");
                    city.put("childList",areaList);
                }
                province.put("childList",cityList);
            }
            redisCache.setCacheList("areaList", provinceList);
        }
        return provinceList;

    }
}
